package com.summer.web.controller;

import com.summer.pojo.Book;
import com.summer.pojo.Order;
import com.summer.service.BookService;
import com.summer.service.OrderService;
import com.summer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @RequestMapping("/addOrder")
    public String addOrder(Order order,@RequestParam int counts){
        System.out.println(order);
        orderService.addOrder(order);
        int bookid=order.getBookid();
        System.out.println(bookid);
        bookService.updateBookById(bookid,counts);
        return "/userOrder";
    }
    @RequestMapping("/userOrder")
    public String userOrder(int uid, Model model){
        List<Order> orders = orderService.selectOrderByUid(uid);
        model.addAttribute("orders",orders);
        return "userOrder";
    }
    @RequestMapping("/userdetail")
    public String userdetail(int bookid, Model model){
        Book book = bookService.findBookById(bookid);
        model.addAttribute("book1",book);
        return "bookdetail";
    }
}
